package com.lcf.system.controller;

import com.lcf.system.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 *
 * @author lcf
 */
@Tag(name = "文件上传")
@RequestMapping("/file")
@RestController
public class FileUploadController {
    private final FileUploadService uploadService;

    public FileUploadController(FileUploadService uploadService) {
        this.uploadService = uploadService;
    }

    /**
     * 上传图片
     */
    @Operation(summary = "上传图片")
    @PostMapping("/img")
    public String uploadImg(@Schema(description = "图片") MultipartFile file,
                            @Schema(description = "上传路径") @Validated @NotBlank(message = "上传路径为空") String path,
                            @Schema(description = "是否压缩（1是0否，默认0）") Integer isCompress) {
        return uploadService.uploadImg(path, file, isCompress != null && isCompress == 1);
    }

    /**
     * 上传文件
     */
    @Operation(summary = "上传文件")
    @PostMapping("/file")
    public String uploadFile(@Schema(description = "文件") MultipartFile file,
                             @Schema(description = "上传路径") @Validated @NotBlank(message = "上传路径为空") String path) {
        return uploadService.uploadFile(path, file);
    }
}
